package com.example.ymm.modules1.controller.common;

import com.example.ymm.model.CommonResult;
import com.example.ymm.model.UserLogin;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.entity.my.user.DbFile;
import com.example.ymm.modules1.service.dd.DingFileService;
import com.example.ymm.modules1.service.user.DbFileService;
import com.example.ymm.util.CosUtil;
import com.example.ymm.util.DateKit;
import com.example.ymm.util.FileUtil;

import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;

@RestController
@RequestMapping("/hl/file")
@Tag(name = "文件管理")
@Slf4j
public class FileController extends BaseController {

    @Autowired
    CosUtil cosUtil;

    @Autowired
    DingFileService dingFileService;

    @Autowired
    DbFileService dbFileService;

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @Schema( name =  "文件上传")
    public CommonResult fileUpload(@RequestParam("file") MultipartFile file) throws IOException {
        try {
            String uploadPath = "H:read_book_server/app_file/images/user";
            // String path=uploadPath+ File.separator + "upload1" + File.separator
            // +file.getOriginalFilename();
            String dates = DateKit.dateFormat(new Date(), "yyyy-MM-dd");

            String path = uploadPath + "/" + dates + "/" + file.getOriginalFilename().replaceAll("\\[B@","");
            File destination = new File( path);
            if (!destination.getParentFile().exists()) {
                FileUtils.forceMkdirParent(destination); // 使用commons-io的工具类
            }
           // file.transferTo(destination);
            FileUtils.copyInputStreamToFile(file.getInputStream(),destination);
            return CommonResult.success(path);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    // @RequestMapping(value = "/objectUpload", method = RequestMethod.POST)
    // @Schema( name =  "腾讯云对象存储图片上传")
    // public CommonResult cosUpload(@RequestParam("file") MultipartFile file)
    // throws IOException {
    // try{
    // String path=null;
    // byte[] bytes = file.getBytes();
    // int contentLength = bytes.length;
    // path=cosUtil.uploadImgFromStream(file.getInputStream(),file.getOriginalFilename(),contentLength);
    // return CommonResult.success(path);
    // }catch (Exception e){
    // log.error(e.getMessage(),e);
    // return CommonResult.error(e.getMessage());
    // }
    // }

    @RequestMapping(value = "/ossUpload", method = RequestMethod.POST)
    @Schema( name =  "阿里云对象存储文件上传")
    public CommonResult cosUpload(@RequestParam("file") MultipartFile file) throws IOException {
        try {
            String path = null;
            //OssUtil.uploadFileByMultipartFile
            // FileUtil.uploadImgToLocal
            path = FileUtil.uploadImgToLocal(file);
            return CommonResult.success(path);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/ossImgUpload", method = RequestMethod.POST)
    @Schema( name =  "阿里云对象存储文件上传")
    public CommonResult ossImgUpload(@RequestParam("file") MultipartFile file) throws IOException {
        try {
            String path = null;
            // path= OssUtil.uploadImgByMultipartFile(file);
            path = FileUtil.uploadImgToLocal(file);

            return CommonResult.success(path);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @RequestMapping(value = "/dingUpload", method = RequestMethod.POST)
    @Schema( name =  "钉盘上传")
    public CommonResult dingUpload(@RequestParam("file") MultipartFile file) throws IOException {
        try {
            String path = null;
            // path= OssUtil.uploadImgByMultipartFile(file);
            path = dingFileService.uploadFileFn(file);

            return CommonResult.success(path);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @RequestMapping(value = "/deleteLocalFile", method = RequestMethod.POST)
    @Schema( name =  "文件删除")
    public CommonResult deleteLocalFile(String url) throws IOException {
        try {
            String s = FileUtil.deleteLocalFile(url);
            return CommonResult.success(s);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }





    @RequestMapping(value = "/file_upload", method = RequestMethod.POST)
    @Schema( name =  "文件上传")
    public CommonResult file_upload(@RequestParam("file") MultipartFile file) throws IOException {
        try {
            String uploadPath = "H:read_book_server/app_file/images/user";
            // String path=uploadPath+ File.separator + "upload1" + File.separator
            // +file.getOriginalFilename();
            String dates = DateKit.dateFormat(new Date(), "yyyy-MM-dd");

            String path = uploadPath + "/" + dates + "/" + file.getOriginalFilename();
            File destination = new File( path);
            if (!destination.getParentFile().exists()) {
                FileUtils.forceMkdirParent(destination); // 使用commons-io的工具类
            }
            // file.transferTo(destination);
            FileUtils.copyInputStreamToFile(file.getInputStream(),destination);
            DbFile dbFile=new DbFile();
            dbFile.setName(file.getOriginalFilename());
            UserLogin userLogin = getUserLogin();
            dbFile.setUserId(userLogin.getId());
            dbFile.setImgUrl(path);
            dbFile.setUserName(userLogin.getUserName());
            DbFile dbFile1 = dbFileService.addFn(dbFile);
            return CommonResult.success(dbFile1);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

}
